home *** CD-ROM | disk | FTP | other *** search
/ Your Choice 3 / Your Choice Software Collection 3.iso / prgmming / swag05 / crc.swg < prev    next >
Encoding:
Text File  |  1994-09-22  |  3.1 KB  |  1 lines

  1. SWAGOLX.EXE (c) 1993 GDSOFT  ALL RIGHTS RESERVED 00002                                                                           1      05-25-9408:03ALL                      DON PAULSEN              Fast 16bit CRC           SWAG9405            17     N0   π{πRE:     SWAG submissionπ        This 16-bit CRC function is compatible with those used in Chuckπ        Forsberg's X-modem protocol.  It's very fast, because I unrolledπ        the "for (i = 0; i < 8; ++i)" loop.  If a 32-bit CRC is notπ        necessary, this is a great alternative because of its speed andπ        small size.ππππ{==============================================================}πFUNCTION Crc16 (var buffer; size, seed: word): word; assembler;ππ{ Set size parameter to 0 to process 64K.  If processing only one buffer, setπ  seed parameter to 0 -- otherwise set to result from previous calculation.π  C code translated by Don Paulsen. }ππ(* This routine is a translation of the following C code by Chuck Forsberg.π   The added "seed" parameter allows for finding the CRC value of data spanningπ   multiple buffers.  The innermost loop has been unrolled at a cost of 32π   bytes in code, but the speed increase is nearly two-fold.ππ     int    Crc16 (ptr, count)π     char   *ptr;π     int    count;ππ     {   int crc, i;ππ         crc = 0;π         while (--count >= 0) {π            crc = crc ^ (int)*ptr++ << 8;π            for (i = 0; i < 8; ++i)π                if (crc & 0x8000)π                    crc = crc << 1 ^ 0x1021;π                elseπ                    crc = crc << 1;π          }π         return (crc & 0xFFFF);π     }π*)ππASMπ    les     di, bufferπ    mov     dx, sizeπ    mov     ax, seedπ    mov     si, 1021hπ@next:π    xor     bl, blπ    mov     bh, es:[di]π    xor     ax, bxππ    shl  ax, 1;    jnc  @noXor1;    xor  ax, siπ@noXor1:π    shl  ax, 1;    jnc  @noXor2;    xor  ax, siπ@noXor2:π    shl  ax, 1;    jnc  @noXor3;    xor  ax, siπ@noXor3:π    shl  ax, 1;    jnc  @noXor4;    xor  ax, siπ@noXor4:π    shl  ax, 1;    jnc  @noXor5;    xor  ax, siπ@noXor5:π    shl  ax, 1;    jnc  @noXor6;    xor  ax, siπ@noXor6:π    shl  ax, 1;    jnc  @noXor7;    xor  ax, siπ@noXor7:π    shl  ax, 1;    jnc  @noXor8;    xor  ax, siπ@noXor8:ππ    inc     diπ    dec     dxπ    jnz     @nextπEND;ππ               2      05-26-9406:16ALL                      DAVID DUNSON             Normalize CRC CalculationSWAG9405            7      N0   π{πFrom what I gather from these two routines, in order to "Normalize" a crcπvalue, you must reverse the order of the four bytes in the value.ππExample: crc value =  $01020304π         normalized = $04030201ππAm I correct in assuming this?ππIf so, the two procedures above fail to perform that task, so here is a BASMπroutine that I have tested and works perfectly.π}ππProcedure Normalize(Var crc: LongInt); Assembler;πASMπ     LES   DI, crcπ     MOV   AX, WORD PTR ES:[DI]π     MOV   BX, WORD PTR ES:[DI + 2]π     XCHG  AH, ALπ     XCHG  BH, BLπ     MOV   WORD PTR ES:[DI + 2], AXπ     MOV   WORD PTR ES:[DI], BXπEnd;ππPlease forward a copy of your response to Serge Paquin who wrote the originalπrequest for CRC routines.π